home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / CodeWarrior Lite / Metrowerks C⁄C++ Lite / Headers / Project Stationery Support / <replace me Mac>.cp < prev    next >
Encoding:
Text File  |  1994-11-25  |  5.7 KB  |  212 lines  |  [TEXT/MMCC]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    Simple Color QuickDraw Sample Application
  6. #
  7. #    SillyBalls
  8. #
  9. #    SillyBalls.c    -    C Source
  10. #
  11. #    Copyright © 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #
  16. #    Components:    SillyBalls.c        August 1, 1988
  17. #                SillyBalls.make        August 1, 1988
  18. #
  19. #    This is a very simple sample program that demonstrates how to use Color 
  20. #    QuickDraw.  It is about two pages of code, and does nothing more than open
  21. #    a color window and draw randomly colored ovals in the window.
  22. #    
  23. #    The purpose is to show how to get some initial results with Color QuickDraw.
  24. #    It is a complete program and is very short to be as clear as possible.
  25. #    
  26. #    It does not have an Event Loop.  It is not fully functional in the sense that
  27. #    it does not do all the things you would expect a well behaved Macintosh 
  28. #    program to do, like size the window naturally, have an event loop, use menus, 
  29. #    etc.
  30. #
  31. #    See Sample and TESample for the general structure and MultiFinder techniques that
  32. #    we recommend that you use when building a new application.
  33. #
  34. ------------------------------------------------------------------------------*/
  35.  
  36.     
  37. //    Version 1.0:    6/2/88
  38. //                    7/20/88     DJB    Converted to C
  39. //    
  40. //    purpose        To demonstrate a simple color App using Color QuickDraw.
  41. //                        It draws colored balls in a color window, then uses colored
  42. //                        text inverted in the ball.  The ball location and color is Random.
  43. //                        
  44. //                        This program was written by Bo3b Johnson, 1/88.
  45. //                        
  46. //                        The inverted Bob text was a Skippy Blair special concept,
  47. //                        kept for obvious aesthetic reasons.
  48.  
  49. //MW -cut out some other program descriptions.-
  50.  
  51. //MW ** Metrowerks note **
  52. //   All changed code by Metrowerks is commented by "//MW".
  53. //   There is one type of modification to the original source:
  54. //   • Added argument type and return type to function definitions.
  55. //       In order to pass with extended error checking on.
  56. //    
  57. //   8/31/93 JA
  58.  
  59.  
  60. #include <Types.h>
  61. #include <Memory.h>
  62. #include <Quickdraw.h>
  63. #include <Fonts.h>
  64. #include <Events.h>
  65. #include <Menus.h>
  66. #include <Windows.h>
  67. #include <TextEdit.h>
  68. #include <Dialogs.h>
  69. #include <OSUtils.h>
  70. #include <ToolUtils.h>
  71. #include <SegLoad.h>
  72.  
  73. /* Constants */
  74. #define BallWidth        20
  75. #define BallHeight        20
  76. #define BobSize            8        /* Size of text in each ball */
  77.  
  78. /* Globals */
  79. Rect    windRect;
  80.     
  81. /* Prototypes */
  82. void Initialize(void);
  83. void NewBall(void);
  84.  
  85. // 
  86. //    Main body of program SillyBalls
  87. //
  88.  
  89. //MW specified argument and return type.
  90. void main(void)
  91. {
  92.     Initialize();
  93.     
  94.     do {
  95.         NewBall();
  96.     } while (!Button());
  97.     
  98. }
  99.  
  100. // 
  101. //    Initialize everything for the program, make sure we can run
  102. //
  103.  
  104. //MW specified argument and return type.
  105. void Initialize(void)
  106. {
  107.     WindowPtr    mainPtr;
  108.     OSErr        error;
  109.     SysEnvRec    theWorld;
  110.         
  111.     //
  112.     //    Test the computer to be sure we can do color.  
  113.     //    If not we would crash, which would be bad.  
  114.     //    If we can’t run, just beep and exit.
  115.     //
  116.  
  117.     error = SysEnvirons(1, &theWorld);
  118.     if (theWorld.hasColorQD == false) {
  119.         SysBeep(50);
  120.         ExitToShell();                    /* If no color QD, we must leave. */
  121.     }
  122.     
  123.     /* Initialize all the needed managers. */
  124.     InitGraf(&qd.thePort);
  125.     InitFonts();
  126.     InitWindows();
  127.     InitMenus();
  128.     TEInit();
  129.     InitDialogs(nil);
  130.     InitCursor();
  131.  
  132.     //
  133.     //    To make the Random sequences truly random, we need to make the seed start
  134.     //    at a different number.  An easy way to do this is to put the current time
  135.     //    and date into the seed.  Since it is always incrementing the starting seed
  136.     //    will always be different.  Don’t for each call of Random, or the sequence
  137.     //    will no longer be random.  Only needed once, here in the init.
  138.     //
  139.     GetDateTime((unsigned long*) &qd.randSeed);
  140.  
  141.     //
  142.     //    Make a new window for drawing in, and it must be a color window.  
  143.     //    The window is full screen size, made smaller to make it more visible.
  144.     //
  145.     windRect = qd.screenBits.bounds;
  146.     InsetRect(&windRect, 50, 50);
  147.     mainPtr = NewCWindow(nil, &windRect, "\pBob Land", true, documentProc, 
  148.                         (WindowPtr) -1, false, 0);
  149.         
  150.     SetPort(mainPtr);                        /* set window to current graf port */
  151.     TextSize(BobSize);                        /* smaller font for drawing. */
  152. }
  153.  
  154.  
  155. // 
  156. //    NewBall: make another ball in the window at a random location and color.
  157. //
  158.  
  159. //MW -specified argument and return type.-
  160. void NewBall(void)
  161. {
  162.     RGBColor    ballColor;
  163.     Rect        ballRect;
  164.     long int    newLeft,
  165.                 newTop;
  166.     
  167.     // 
  168.     //    Make a random new color for the ball.
  169.     //
  170.     ballColor.red   = Random();
  171.     ballColor.green = Random();
  172.     ballColor.blue  = Random();
  173.     
  174.     // 
  175.     //    Set that color as the new color to use in drawing.
  176.     //
  177.     RGBForeColor (&ballColor);
  178.  
  179.     //    
  180.     //    Make a Random new location for the ball, that is normalized to the window size.  
  181.     //    This makes the Integer from Random into a number that is 0..windRect.bottom
  182.     //    and 0..windRect.right.  They are normalized so that we don't spend most of our
  183.     //    time drawing in places outside of the window.
  184.     //
  185.     newTop = Random();    newLeft = Random();
  186.     newTop = ((newTop+32767) * windRect.bottom)/65536;
  187.     newLeft = ((newLeft+32767) * windRect.right)/65536;
  188.     SetRect(&ballRect, newLeft, newTop, newLeft+BallWidth, newTop+BallHeight);
  189.     
  190.     //
  191.     //    Move pen to the new location, and paint the colored ball.
  192.     //
  193.     MoveTo(newLeft, newTop);
  194.     PaintOval (&ballRect);
  195.     
  196.     //
  197.     //    Move the pen to the middle of the new ball position, for the text
  198.     //
  199.     MoveTo(ballRect.left + BallWidth/2 - BobSize, 
  200.         ballRect.top + BallHeight/2 + BobSize/2 -1);
  201.     
  202.     //    
  203.     //    Invert the color and draw the text there.  This won’t look quite right in 1 bit
  204.     //    mode, since the foreground and background colors will be the same.
  205.     //    Color QuickDraw special cases this to not invert the color, to avoid
  206.     //    invisible drawing.
  207.     //
  208.     InvertColor(&ballColor); 
  209.     RGBForeColor(&ballColor);
  210.     DrawString("\pBob");
  211. }
  212.